package vip.xiaonuo.smzq.modular.dictionary.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb04Kszl;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb14Kcp;
import vip.xiaonuo.smzq.modular.dictionary.entity.vo.KcNameTree;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb04KszlMapper;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb14KcpMapper;
import vip.xiaonuo.smzq.modular.dictionary.service.ICb14KcpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 矿产品 服务实现类
 *
 * @author eomer
 * @since 2024-02-21
 */
@Service
public class Cb14KcpServiceImpl extends ServiceImpl<Cb14KcpMapper, Cb14Kcp> implements ICb14KcpService {

    @Resource
    private Cb14KcpMapper cb14KcpMapper;
    @Resource
    private Cb04KszlMapper cb04KszlMapper;//矿石种类

    @Override
    public Page<Cb14Kcp> page(Cb14Kcp cb14Kcp) {
        QueryWrapper<Cb14Kcp> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    /**
     * 矿产品下拉树
     */
    @Override
    public KcNameTree getKcpDirTree() {
        KcNameTree dirTree=new KcNameTree();
        dirTree.setId("0");
        dirTree.setName("矿产名称");
        dirTree.setPid("0");
        dirTree.setChildren(getSecondTree());
        return dirTree;
    }


    private List<KcNameTree> getSecondTree(){
        List<KcNameTree> kcNameTreeList = new ArrayList<>();
        List<String> kcmcList=cb14KcpMapper.getKcmcList();

        for (String str:kcmcList) {
            KcNameTree kcNameTree = new KcNameTree();
            //矿产 编码
            String kcdm = str.substring(0, str.indexOf("*"));
            //矿产 名称
            String kcmc = str.substring(str.indexOf("*") + 1);
            kcNameTree.setName(kcmc);
            kcNameTree.setId(kcdm);
            kcNameTree.setPid("0");
            kcNameTree.setKcdm(Integer.valueOf(kcdm));
            //矿石质量筛选下拉框
            kcNameTree.setCb04KszlList(getKszlList(kcdm));

            kcNameTree.setChildren(getThirdTree(kcdm));
            kcNameTreeList.add(kcNameTree);
        }
        return kcNameTreeList;
    }


    private List<KcNameTree> getThirdTree(String kcdm){
        List<KcNameTree> kcNameTreeList = new ArrayList<>();
        QueryWrapper<Cb14Kcp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("kcdm", Integer.valueOf(kcdm));
        List<Cb14Kcp> cb14KcpList = cb14KcpMapper.selectList(queryWrapper);
        for (Cb14Kcp cb14Kcp:cb14KcpList) {
            KcNameTree kcNameTree=new KcNameTree();
            kcNameTree.setName(cb14Kcp.getKcpmc());
            kcNameTree.setPid(kcdm);
            kcNameTree.setId(cb14Kcp.getKcpdm());
            kcNameTree.setKsldw(cb14Kcp.getKcpdm());
            kcNameTree.setKsldw(cb14Kcp.getKcpdw());
            kcNameTreeList.add(kcNameTree);
        }
        return kcNameTreeList;
    }






    /**
     * 矿石质量筛选下拉框
     * kqdm 矿区代码
     */
    public List<Cb04Kszl> getKszlList(String kcdm) {
        QueryWrapper<Cb04Kszl> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("kcdm", Integer.valueOf(kcdm) );
        return cb04KszlMapper.selectList(queryWrapper);
    }

    /**
     * 矿产品下拉树-重构
     *
     * @author Kevin
     * @since 2024/7/23 16:47
     */
    @Override
    public KcNameTree getKcpTree() {

        KcNameTree tree = new KcNameTree();
        tree.setId("0");
        tree.setName("矿产名称");
        tree.setPid("0");

        //查询全部矿产品与其对应的矿石质量
        List<Cb04Kszl> kszlList = cb04KszlMapper.selectList(new QueryWrapper<>());

        //根据矿产代码分组
        Map<String, List<Cb04Kszl>> kszlGroupMap = kszlList.stream().collect(Collectors.groupingBy(o -> String.valueOf(o.getKcdm())));

        //查询全部矿产品
        LambdaQueryWrapper<Cb14Kcp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Cb14Kcp::getKcdm, Cb14Kcp::getKcmc);
        List<Cb14Kcp> kcpList = cb14KcpMapper.selectList(queryWrapper);

        //根据矿产代码分组
        Map<String, List<Cb14Kcp>> groupMap = kcpList.stream().collect(Collectors.groupingBy(o -> String.valueOf(o.getKcdm()).replace(".0", "")));

        //遍历分组后的Map
        List<KcNameTree> kcNameTreeList = new ArrayList<>();
        for (Map.Entry<String, List<Cb14Kcp>> entry : groupMap.entrySet()) {
            List<Cb14Kcp> cpList = entry.getValue();
            String kcdm = entry.getKey();
            KcNameTree treeNode = new KcNameTree();
            treeNode.setId(kcdm);
            treeNode.setPid("0");
            if (CollectionUtil.isNotEmpty(cpList)) {
                treeNode.setName(cpList.get(0).getKcmc());
            }
            treeNode.setKcdm(Integer.valueOf(kcdm));
            //挂接矿石质量列表
            List<Cb04Kszl> kszls = kszlGroupMap.get(entry.getKey());
            treeNode.setCb04KszlList(kszls);
            //组织子集
            if (CollectionUtil.isNotEmpty(cpList)) {
                List<KcNameTree> childList = cpList.stream()
                        .map(item -> new KcNameTree() {{
                            setId(String.valueOf(item.getKcpdm()));
                            setPid(kcdm);
                            setName(item.getKcpmc());
                            setKsldw(item.getKcpdw());
                        }}).collect(Collectors.toList());
                treeNode.setChildren(childList);
            }
            kcNameTreeList.add(treeNode);
        }
        //将kcNameTreeList根据kcdm进行排序
        kcNameTreeList.sort(Comparator.comparing(KcNameTree::getKcdm));
        tree.setChildren(kcNameTreeList);
        return tree;
    }
}
